#pragma once

#include <QMap>
#include <QPointer>
#include <QWidget>
#include <set>

#include "../../IMainFrame.h"
#include "TabBar.h"

class TitleBar;
class TabBar;
class StatusBarWorke;
class QStackedWidget;
class WidgetResizer;
class SBaseDialog;
class QTimer;
class QJsonObject;
class BrowserDialog;

class MainFrame : public QWidget, public IMainFrame {
  Q_OBJECT
 public:
  MainFrame();
  ~MainFrame() = default;

  bool eventFilter(QObject* watched, QEvent* event) override;

  // 通过 IMainFrame 继承
  int existCmdWindow(int cmdId, const QString& params,
                     bool enableParam = true) override;
  int openCmdWindow(int cmdId, const QString& params, QWidget* widget,
                    const QString& title, bool popup = false) override;
  void activateCmdWindow(int tabId) override;
  void updateWindowTitle(int windowId, const QString& title) override;

  virtual void setCommonPageNotify(int* ids, int size, int ntype) override;
  virtual void setIasMenuNotify(const QString& iasmenu) override;
  virtual void addStatusbarItem(QWidget* btn,
                                QWidget* before = nullptr) override;

  KeySearchWidget* searchWidget() override;
  QWidget* cmdWindow(int tabid) override;

  TabBar* tabBar() const { return m_tabBar; }
  void setTabBar(TabBar* tb) { m_tabBar = tb; }

  float uiScale() const { return m_uiScale; }

  void openAccountInfo(const QString& tag);

 protected:
  void paintEvent(QPaintEvent* event) override;

 signals:
  void scaleChanged(float scale);
  void setConfigAuth(int* ids, int size);
  void setCommonPage(int* ids, int size, int ntype);
 private slots:
  void onTabClose(int tabId);
  void onTabActivated(int newId);
  void onTabSeperated(CmdWindowInfo ti);
  void onPopupMoving(SBaseDialog* dlg);
  void onPoupuClosed();
  void onRelationCmdClick();
  void openHome();
  void onServiceClicked();
  void onHelpClicked();

 private:
  void initializeCmdRelations();
  void updateRelationBar(int activeCmd);
  void closeWindow(int wid);
  void onScaleChanged(float scale);

 private:
  TitleBar* m_title = nullptr;
  TabBar* m_tabBar = nullptr;
  QWidget* m_relatedCmdBar = nullptr;
  QStackedWidget* m_contentWidget = nullptr;
  QWidget* m_curContentChild = nullptr;
  WidgetResizer* m_resizer = nullptr;
  QMap<int, CmdWindowInfo>
      m_cmdWindows;  // place it in command mgr?<winid, cmdinf>
  QTimer* m_timeTimer = nullptr;
  QVector<QVector<int> > m_relationCmds;
  QVector<int> m_curRelations;
  float m_uiScale = 1;

  QPointer<BrowserDialog> m_helpDlg;
  QPointer<BrowserDialog> m_sevcDlg;
  int m_currentTabId{-1};
};
